/**
 * 
 */
package org.iridium.allcomments.buzz;

import java.util.HashSet;

import org.iridium.allcomments.common.Constants;
import org.iridium.allcomments.dto.CommentDTO;
import org.iridium.allcomments.persistance.CommentsDAO;
import org.iridium.allcomments.persistance.CommentsDAOImpl;
import org.iridium.allcomments.persistance.UserTagDAO;
import org.iridium.allcomments.persistance.UserTagDAOImpl;

/**
 * @author iridium
 * 
 */
public class CommentBuzz {
	/**
	 * add a comment, will check the tags whether be contained in table user_tag
	 * 
	 * @param comment
	 */
	public void addComment(CommentDTO comment) {
		long userId = comment.getUserId();
		HashSet<String> tags = comment.getTags();

		CommentsDAO commDao = new CommentsDAOImpl();
		commDao.addItem(userId, comment.getCommentId(), comment
				.getPreCommentId(), comment.getUrl(), comment.getContent(),
				comment.getIp());

		UserTagDAO tagDao = new UserTagDAOImpl();
		for (String tag : tags) {
			if (tagDao.existsTag(userId, tag))
				continue;
			tagDao.addItem(userId, tag, Constants.VISIBILITY_PUBLIC);
		}
	}

	/**
	 * to remove a comment according to its ID
	 * 
	 * @param commentId
	 */
	public void removeComment(String commentId) {
		CommentsDAO commDao = new CommentsDAOImpl();
		commDao.delItem(commentId);
	}

	/**
	 * 
	 * modify a comment using a comment DTO 1. modify content of comment 2.
	 * modify tags
	 * 
	 * @param comment
	 */
	public void modifyComment(CommentDTO comment) {
		String commentId = comment.getCommentId();
		HashSet<String> tags = comment.getTags();

		CommentsDAO commDao = new CommentsDAOImpl();
		commDao.updateContent(commentId, comment.getContent());
		commDao.updateTags(commentId, tags);

		UserTagDAO tagDao = new UserTagDAOImpl();
		for (String tag : tags) {
			if (tagDao.existsTag(comment.getUserId(), tag)) {
				continue;
			}
			tagDao.addItem(comment.getUserId(), tag,
					Constants.VISIBILITY_PUBLIC);
		}
	}

}
